package com.yw.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yw.dataStandardCatalog.domain.DataStandardCatalog;
import com.yw.dataStandardCatalog.dto.AddDataStandardCatalogDTO;
import com.yw.dataStandardCatalog.vo.CatalogVO;
import com.yw.dataStandardCatalog.dto.DataStandardCatalogDTO;
import com.yw.dataStandardCatalog.dto.UpdateDataStandardCatalogDTO;
import com.yw.dataStandardCatalog.vo.ClickOnStandardLableVO;
import com.yw.dataStandardCatalog.vo.DataStandardCatalogVO;
import com.yw.mapper.DataStandardCatalogMapper;
import com.yw.piliangDTO.StatusDTO;
import com.yw.service.DataStandardCatalogService;
import com.yw.utils.BeanConverter;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;


import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class DataStandardCatalogServiceImpl extends ServiceImpl<DataStandardCatalogMapper, DataStandardCatalog>
                                            implements DataStandardCatalogService {

    @Resource
    private DataStandardCatalogMapper dataStandardCatalogMapper;


    @Override
    public Page<DataStandardCatalogVO> pageAllByQuery(DataStandardCatalogDTO dataStandardCatalogDTO) {
        if (dataStandardCatalogDTO == null){

        }
        Page<DataStandardCatalog> page = new Page<>(dataStandardCatalogDTO.getPageNumber(), dataStandardCatalogDTO.getPageSize());
        LambdaQueryWrapper<DataStandardCatalog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(dataStandardCatalogDTO.getSourceOrgan() != null,DataStandardCatalog::getSourceOrgan,dataStandardCatalogDTO.getSourceOrgan())
                .eq(dataStandardCatalogDTO.getStandardState()!=null,DataStandardCatalog::getStandardState,dataStandardCatalogDTO.getStandardState())
                .like(dataStandardCatalogDTO.getStandardCode()!=null,DataStandardCatalog::getStandardCode,dataStandardCatalogDTO.getStandardCode())
                .like(dataStandardCatalogDTO.getCName()!=null,DataStandardCatalog::getCName,dataStandardCatalogDTO.getCName())
                .like(dataStandardCatalogDTO.getEName()!=null,DataStandardCatalog::getEName,dataStandardCatalogDTO.getEName());
        Page<DataStandardCatalog> resultPage = this.page(page, wrapper);
        Page<DataStandardCatalogVO> returnPage = new Page<>();
        returnPage.setRecords(BeanConverter.convertList(resultPage.getRecords(),DataStandardCatalogVO.class));
        returnPage.setSize(returnPage.getSize());
        returnPage.setTotal(resultPage.getTotal());
        returnPage.setCurrent(returnPage.getCurrent());
        returnPage.setPages(resultPage.getPages());
        return returnPage;
    }

    @Override
    public ClickOnStandardLableVO ClickOnOne(Integer id) {
        LambdaQueryWrapper<DataStandardCatalog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DataStandardCatalog::getId,id);
        DataStandardCatalog one = this.getOne(wrapper);
        ClickOnStandardLableVO clickOnStandardLableVO = new ClickOnStandardLableVO();
        clickOnStandardLableVO.setStandardCode(one.getStandardCode());
        clickOnStandardLableVO.setCName(one.getCName());
        clickOnStandardLableVO.setEName(one.getEName());
        clickOnStandardLableVO.setStandardDesc(one.getStandardDesc());
        clickOnStandardLableVO.setSourceOrgan(one.getSourceOrgan());
        clickOnStandardLableVO.setNumberType(one.getNumberType());
        clickOnStandardLableVO.setCode(one.getCode());
        clickOnStandardLableVO.setDefaultNum(one.getDefaultNum());
        return clickOnStandardLableVO;
    }

    @Override
    public Boolean addDataStandard(AddDataStandardCatalogDTO addDataStandardCatalogDTO) {
        DataStandardCatalog dataStandardCatalog = new DataStandardCatalog();
        dataStandardCatalog.setCName(addDataStandardCatalogDTO.getCName());
        dataStandardCatalog.setEName(addDataStandardCatalogDTO.getEName());
        dataStandardCatalog.setStandardDesc(addDataStandardCatalogDTO.getStandardDesc());
        dataStandardCatalog.setSourceOrgan(addDataStandardCatalogDTO.getSourceOrgan());
        dataStandardCatalog.setNumberType(addDataStandardCatalogDTO.getNumberType());
        switch (addDataStandardCatalogDTO.getNumberType()){
            case "Int":
                dataStandardCatalog.setValueRange(String.valueOf(addDataStandardCatalogDTO.getMin() + "~" +String.valueOf(addDataStandardCatalogDTO.getMax())));
                dataStandardCatalog.setDefaultNum("1");
                break;
            case "Flout":
                dataStandardCatalog.setValueRange(String.valueOf(addDataStandardCatalogDTO.getMin() + "~" +String.valueOf(addDataStandardCatalogDTO.getMax())));
                dataStandardCatalog.setNumAccuracy(addDataStandardCatalogDTO.getNumAccuracy());
                dataStandardCatalog.setDefaultNum("9.9");
                break;
            case "String":
                dataStandardCatalog.setLength(addDataStandardCatalogDTO.getLength());
                dataStandardCatalog.setDefaultNum("无法描述");
                break;
            case "Enum":
                dataStandardCatalog.setCode(addDataStandardCatalogDTO.getCode());
                dataStandardCatalog.setDefaultNum("01");
                break;
        }
        dataStandardCatalog.setIsEnable(0);
        dataStandardCatalog.setStandardState(0);
        dataStandardCatalog.setCreateTime(LocalDateTime.now());
        dataStandardCatalog.setUpdateTime(LocalDateTime.now());
        LambdaQueryWrapper<DataStandardCatalog> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(DataStandardCatalog::getId).last("LIMIT 1");;
        DataStandardCatalog one = this.getOne(wrapper);
        String numString = one.getStandardCode().substring(2, 7);
        Integer i = Integer.valueOf(numString);
        int num = i + 1;
        String resultCode = null;
        switch (String.valueOf(num).length()){
            case 1: resultCode = "BZ0000" + num;break;
            case 2: resultCode = "BZ000" + num;break;
            case 3: resultCode = "BZ00" + num;break;
            case 4: resultCode = "BZ0" + num;break;
            case 5: resultCode = "BZ" + num;break;
        }
        dataStandardCatalog.setStandardCode(resultCode);
        if (this.save(dataStandardCatalog)){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Boolean updateDataStandard(UpdateDataStandardCatalogDTO updateDataStandardCatalogDTO) {
        DataStandardCatalog dataStandardCatalog = new DataStandardCatalog();
        dataStandardCatalog.setId(updateDataStandardCatalogDTO.getId());
        DataStandardCatalog two = this.getById(updateDataStandardCatalogDTO.getId());
        dataStandardCatalog.setCName(updateDataStandardCatalogDTO.getCName());
        dataStandardCatalog.setEName(updateDataStandardCatalogDTO.getEName());
        dataStandardCatalog.setStandardDesc(updateDataStandardCatalogDTO.getStandardDesc());
        dataStandardCatalog.setSourceOrgan(updateDataStandardCatalogDTO.getSourceOrgan());
        dataStandardCatalog.setNumberType(updateDataStandardCatalogDTO.getNumberType());
        switch (updateDataStandardCatalogDTO.getNumberType()){
            case "Int":
                dataStandardCatalog.setValueRange(String.valueOf(updateDataStandardCatalogDTO.getMin() + "~" +String.valueOf(updateDataStandardCatalogDTO.getMax())));
                dataStandardCatalog.setDefaultNum(updateDataStandardCatalogDTO.getDefaultNum());
                break;
            case "Flout":
                dataStandardCatalog.setValueRange(String.valueOf(updateDataStandardCatalogDTO.getMin() + "~" +String.valueOf(updateDataStandardCatalogDTO.getMax())));
                dataStandardCatalog.setNumAccuracy(updateDataStandardCatalogDTO.getNumAccuracy());
                dataStandardCatalog.setDefaultNum(updateDataStandardCatalogDTO.getDefaultNum());
                break;
            case "String":
                dataStandardCatalog.setLength(updateDataStandardCatalogDTO.getLength());
                dataStandardCatalog.setDefaultNum(updateDataStandardCatalogDTO.getDefaultNum());
                break;
            case "Enum":
                dataStandardCatalog.setCode(updateDataStandardCatalogDTO.getCode());
                dataStandardCatalog.setDefaultNum(updateDataStandardCatalogDTO.getDefaultNum());
                break;
        }
        dataStandardCatalog.setIsEnable(0);
        dataStandardCatalog.setStandardState(0);
        dataStandardCatalog.setCreateTime(two.getCreateTime());
        dataStandardCatalog.setUpdateTime(LocalDateTime.now());
        LambdaQueryWrapper<DataStandardCatalog> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(DataStandardCatalog::getId).last("LIMIT 1");;
        DataStandardCatalog one = this.getOne(wrapper);
        dataStandardCatalog.setStandardCode(one.getStandardCode());
        this.removeById(updateDataStandardCatalogDTO.getId());
        if (this.save(dataStandardCatalog)){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Boolean updateStatic(Integer id) {
        DataStandardCatalog one = this.getById(id);
        switch (one.getStandardState()){
            case 0, 2:
                one.setStandardState(1);break;
            case 1:
                one.setStandardState(2);break;
        }
        if(this.updateById(one)){
            return true;
        }else {
            return false;
        }
    }

    //批量发布或停用
    @Override
    public Boolean batch(StatusDTO statusDTO) {
        return dataStandardCatalogMapper.batch(statusDTO);
    }
    //数据资产管理中添加时点击标准对应的接口

    @Override
    public List<CatalogVO> all() {
        List<DataStandardCatalog> list = this.list();
        ArrayList<CatalogVO> all = new ArrayList<>();
        for (DataStandardCatalog dataStandardCatalog : list) {
            String result = null;
            result = dataStandardCatalog.getStandardCode() + "  " + dataStandardCatalog.getCName() + "   " + dataStandardCatalog.getEName();
            CatalogVO catalogVO = new CatalogVO();
            catalogVO.setName(result);
            catalogVO.setId(dataStandardCatalog.getId());
            all.add(catalogVO);
        }
        return all;
    }

}
